package com.cskaoyan.day2.collection._03_example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

/**
 * // 1.存储一组学生。包括 姓名 年龄，总分，入学日期
 * // 2.找出总分最高的学生？
 * // 3.找出总分最低的学生
 * // 4.求平均分
 * // 5.删除低于平均分的学生
 * // 6.找出2年内入学的学生。
 * // 7.需要删除叫张三的学生
 * // 8.删除年龄小于18的学生
 *
 * @author Common-zhou
 * @since 2023-02-28 15:49
 */
public class Demo1 {
    public static void main(String[] args) throws ParseException {

        Collection<Student> studentList = StudentList.getStudentList();


        //for (Student student : studentList) {
        //    System.out.println(student);
        //}

        //testFindMaxScore();

        //testFindAvgScore();

        //testDeleteStudentLowAvg();

        //testDeleteStudentLowAvg2();


        //testFindBetween2021EnterDate();

        //testRemoveZs();

        testRemoveAgeLow18();



    }

    private static void testRemoveAgeLow18() throws ParseException {
        //8.删除年龄小于18的学生
        Collection<Student> studentList = StudentList.getStudentList();
        Iterator<Student> iterator = studentList.iterator();

        while (iterator.hasNext()) {
            Student next = iterator.next();

            if (next.getAge() < 18) {
                iterator.remove();
            }

        }

        System.out.println(studentList.size());
        System.out.println(studentList);

        // 1.使用一个集合 ，把这些学生存起来
        //  2.1 removeAll
        // 2.2 遍历这个集合，使用remove方法

    }

    private static void testRemoveZs() throws ParseException {
        // 7.需要删除叫张三的学生
        Collection<Student> studentList = StudentList.getStudentList();

        Iterator<Student> iterator = studentList.iterator();

        while (iterator.hasNext()) {
            Student next = iterator.next();
            if ("zs".equals(next.getName())) {
                // 使用迭代器的时候 不能改变原有的对象
                //studentList.remove(next);
                iterator.remove();
            }
        }

        System.out.println(studentList.size());
        System.out.println(studentList);


    }

    private static void testFindBetween2021EnterDate() throws ParseException {
        // 6.找出2年内入学的学生。
        // 假设 2021-01-01 都算两年内
        Collection<Student> studentList = StudentList.getStudentList();

        Collection<Student> between2021EnterDateStudent = new ArrayList<>();

        Date date = new Date();
        Date date2021 = new SimpleDateFormat("yyyy-MM-dd").parse("2021-01-01");

        //date.after()
        for (Student student : studentList) {
            Date enterDate = student.getEnterDate();
            if (enterDate.after(date2021) && enterDate.before(date)) {
                System.out.println(student);

                between2021EnterDateStudent.add(student);

            }
        }

    }

    private static void testDeleteStudentLowAvg2() throws ParseException {
        // 5.删除低于平均分的学生

        Collection<Student> studentList = StudentList.getStudentList();

        int count = 0;
        int allStudentTotalScore = 0;

        for (Student student : studentList) {
            count++;
            allStudentTotalScore += student.getTotalScore();
        }

        double avgScore = 1.0 * allStudentTotalScore / count;

        Collection<Student> needRemovedStudent = new ArrayList<>();

        for (Student student : studentList) {
            if (student.getTotalScore() < avgScore) {
                needRemovedStudent.add(student);
            }
        }

        //studentList.removeAll(needRemovedStudent);

        for (Student student : needRemovedStudent) {
            studentList.remove(student);
        }

        System.out.println(studentList);
        System.out.println(studentList.size());


    }

    private static void testDeleteStudentLowAvg() throws ParseException {
        // 5.删除低于平均分的学生

        Collection<Student> studentList = StudentList.getStudentList();

        int count = 0;
        int allStudentTotalScore = 0;

        for (Student student : studentList) {
            count++;
            allStudentTotalScore += student.getTotalScore();
        }

        double avgScore = 1.0 * allStudentTotalScore / count;


        System.out.println(studentList);
        System.out.println(studentList.size());

        // 2.迭代器遍历，判断是否满足条件
        Iterator<Student> iterator = studentList.iterator();
        while (iterator.hasNext()) {
            Student next = iterator.next();
            if (next.getTotalScore() < avgScore) {
                iterator.remove();
            }
        }

        System.out.println(studentList);
        System.out.println(studentList.size());

    }

    private static void testFindAvgScore() throws ParseException {
        Collection<Student> studentList = StudentList.getStudentList();

        // 平均分 = 总分 / 学生个数
        // 遍历，然后累加总分；累加人数

        // 学生的人数
        int count = 0;
        // 总的分数
        int allStudentTotalScore = 0;

        for (Student student : studentList) {
            count++;
            allStudentTotalScore += student.getTotalScore();
        }

        //
        double avgScore = 1.0 * allStudentTotalScore / count;
        System.out.println(avgScore);


    }

    private static void testFindMaxScore() throws ParseException {
        // 2.找出总分最高的学生？

        Collection<Student> studentList = StudentList.getStudentList();

        // 1.使用一个int值来保留最高分 ， 还需要一个学生变量
        int maxScore = -1;
        Student maxScoreStudent = null;

        // 2. 遍历。
        //      比较哪个大。比较当前学生的分数和 最高分数 ，如果当前学生大，存储下来 最高分和学生信息
        // 3. 打印学生信息

        for (Student student : studentList) {
            int totalScore = student.getTotalScore();
            if (maxScore < totalScore) {
                // 最高的分数
                maxScore = totalScore;
                // maxScoreStudent  存储的是分数最高的学生信息
                maxScoreStudent = student;
            }
        }
        System.out.println(maxScore);
        System.out.println(maxScoreStudent);

    }
}
